昨天已經做了第二個畫面,
今天要做換頁並且把資料帶到第二頁,
我們要通過Intent帶資料過去,
Intent是一個動作與內容的集合,
Intent像是一串網址,
系統會根據接收到的Intent來決定要用什麼載體來處理網址中所指定的動作跟內容。
Intent可以分為兩種類型:現成的Intent與自訂的Intent。
使用現成的Intent即是使用系統或其他應用程式所提供的Intent來協助完成工作。
在Android清單中做設定時,
我們可以使用intent-filter標籤來過濾和找尋對應的Intent,
而一般的開發者在程式中所自行撰寫的Intent也可以透過自訂Intent來做很多事情。
比如切換Activity、在其間傳遞各式的資料。
要完成在Activity之間透過Intent傳送資訊的動作,
可以分成傳遞資料與接收資料兩個部分。
傳遞訊息的方法
MainActivity.java
Intent intent = new Intent();
intent.setClass(MainActivity.this, ReportActivity.class);
Bundle bundle = new Bundle();
bundle.putString("KEY_HEIGHT", num_height.getText().toString());
bundle.putString("KEY_WEIGHT", num_weight.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
在strings.xml新增
<string name="report_back">前一頁</string>
activity_report.xml加入結果的顯示及按鈕
<TextView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceLarge"/>
<TextView
    android:id="@+id/suggest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceLarge"/>
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/report_back" />
ReportActivity.java完整程式
package com.example.user.mybmi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class ReportActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        initViews();
        showResults();
        setListensers();
    }
    private Button button_back;
    private TextView show_result;
    private TextView show_suggest;
    private void initViews()
    {
        button_back = (Button)findViewById(R.id.button);
        show_result = (TextView)findViewById(R.id.result);
        show_suggest = (TextView)findViewById(R.id.suggest);
    }
    private void showResults()
    {
        try
        {
            DecimalFormat nf = new DecimalFormat("0.00");
            Bundle bundle = this.getIntent().getExtras();
            //身高
            double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;
            //體重
            double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
            //計算出BMI值
            double BMI = weight / (height*height);
            //結果
            show_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
            //建議
            if(BMI > 25) //太重了
                show_suggest.setText(R.string.advice_heavy);
            else if(BMI < 20) //太輕了
                show_suggest.setText(R.string.advice_light);
            else //剛剛好
                show_suggest.setText(R.string.advice_average);
        }
        catch(Exception obj)
        {
            Toast.makeText(this, "要先輸入身高體重喔!", Toast.LENGTH_SHORT).show();
        }
    }
    private void setListensers()
    {
        button_back.setOnClickListener(backtoMain);
    }
    private Button.OnClickListener backtoMain = new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            ReportActivity.this.finish();
        }
    };
}
這是一開始的畫面
這是換頁的畫面
明天再來做比較詳細的解說